package org.budo.graph.service.util;

import java.io.InputStream;
import java.util.List;

import org.budo.support.lang.util.IoUtil;
import org.budo.support.lang.util.ListUtil;
import org.budo.support.lang.util.ProcessUtil;
import org.budo.support.lang.util.StringUtil;
import org.budo.support.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.context.ApplicationContext;

/**
 * @author lmw
 */
public class MemoryMapUtil {
    private static final Logger log = Slf4j.getLogger();

    public static String writeMemoryMapResponse(ApplicationContext applicationContext, String url) {
        try {
            List<String> command = ListUtil.toStringList("jmap", "-histo", ProcessUtil.getCurrentProcessId() + "");
            InputStream inputStream = ProcessUtil.runCommand(command);
            return "<pre>" + StringUtil.join(command, " ") + "\n" + IoUtil.toString(inputStream) + "</pre>";
        } catch (Throwable e) {
            log.error("#25 writeMemoryMapResponse error", e);
            return "<pre>" + e + "</pre>";
        }
    }
}